package com.sky.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    SetmealMapper setmealMapper;

    @Autowired
    SetmealDishMapper setmealDishMapper;

    @Autowired
    DishMapper dishMapper;
    /**
     * 菜品分页
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        //开启分页
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());

        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);

        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 新增套餐
     *
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void addSetmealWithDish(SetmealDTO setmealDTO) {
        //利用套餐实体类封装前端发来的数据
        Setmeal setmeal = new Setmeal();
        //利用工具类进行属性赋值
        BeanUtils.copyProperties(setmealDTO, setmeal);

        //向套餐表中插入数据
        setmealMapper.insert(setmeal);

        //获取每次添加生成的id
        Long setmealId = setmeal.getId();

        //获取套餐的中的菜品集合
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
        });

        setmealDishMapper.insertBatch(setmealDishes);


    }

    /**
     * 根据id查询套餐信息
     *
     * @param id
     * @return
     */
    @Override
    public SetmealVO getSetmealById(Long id) {
        Setmeal setmeal = setmealMapper.getSetmealById(id);

        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);

        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);

        setmealVO.setSetmealDishes(setmealDishes);

        return setmealVO;
    }

    /**
     * 修改套餐信息
     *
     * @param setmealDTO
     */
    @Override
    public void update(SetmealDTO setmealDTO) {
        //new一个套餐表
        Setmeal setmeal = new Setmeal();
        //属性赋值
        BeanUtils.copyProperties(setmealDTO, setmeal);

        //修改套餐信息
        setmealMapper.updateSetmeal(setmeal);

        //删除套餐和菜品的关联关系
        setmealDishMapper.deleteSetmeal(setmealDTO.getId());

        //插入新的套餐信息
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() > 0) {
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealDTO.getId());
            });
            //重新插入套餐和菜品的关联关系
            setmealDishMapper.insertBatch(setmealDishes);
        }

    }

    /**
     * 套餐的启售和停售
     *
     * @param id
     * @param status
     */
    @Override
    public void openOrClose(Integer status, Long id) {
        //起售套餐时，判断套餐内是否有停售菜品，有则抛出异常
        if (status == StatusConstant.ENABLE) {
         List<Dish> dishList=dishMapper.getBySetmealId(id);

         if(dishList !=null && dishList.size()>0){
             dishList.forEach(dish -> {
                 if(dish.getStatus()==StatusConstant.DISABLE){
                     throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                 }
             });
         }
        }
        Setmeal setmeal = Setmeal.builder()
                .status(status)
                .id(id)
                .build();

        setmealMapper.updateSetmeal(setmeal);

    }

    /**
     * 批量删除套餐
     * @param ids
     */
    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {
        ids.forEach(id ->{
            Setmeal setmealById = setmealMapper.getSetmealByIds(id);
            if(setmealById.getStatus()==StatusConstant.ENABLE){
                throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });

        ids.forEach(setmealId->{
            //删除套餐表中的数据
            setmealMapper.deleteBatch(setmealId);
            //删除套餐菜品关系表中的数据
            setmealDishMapper.deleteSetmealDish(setmealId);
        });
    }


    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }


    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

}
